import apiClient from "@/lib/interceptor";

// 获取所有其他部门信息
export const getAllOtherDepartments = async () => {
  try {
    const response = await apiClient.get('/department/otherDept');
    // 即使后端返回错误码，也不抛出异常，而是在调用处处理
    return response;
  } catch (error) {
    console.error('获取其他部门信息失败:', error);
    // 返回一个带有错误信息的伪响应，调用处可以处理这个返回
    return {
      data: {
        code: 0,
        msg: '获取其他部门数据失败',
        data: []
      }
    };
  }
};

// 获取单个其他部门信息
export const getOtherDepartment = (otherDeptId: number) => {
  return apiClient.get(`/department/otherDept?otherDeptId=${otherDeptId}`);
};

// 新增其他部门
export const addOtherDepartment = (deptInfo: {
  otherDeptName: string;
  otherDeptManagerId: string;
  otherDeptProfile: string;
  otherDeptNumber: string;
  otherDeptParentId: number;
  otherDeptLevel: number;
}) => {
  return apiClient.post('/department/otherDept', deptInfo);
};

// 更新其他部门信息
export const updateOtherDepartment = (deptInfo: {
  otherDeptId: number;
  otherDeptName?: string;
  otherDeptManagerId?: string;
  otherDeptParentId?: number;
  otherDeptProfile?: string;
  otherDeptNumber?: string;
  otherDeptLevel?: number;
}) => {
  return apiClient.patch('/department/otherDept', deptInfo);
};

// 删除其他部门
export const deleteOtherDepartment = (otherDeptIds: number | number[]) => {
  const ids = Array.isArray(otherDeptIds) ? otherDeptIds.join(',') : otherDeptIds;
  return apiClient.delete(`/department/otherDept/${ids}`);
};

export const getOtherDepts = () => {
  return apiClient.get('/department/otherDept');
};